package com.jcli.novel_p;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;

/**
 * 1.0.分页功能
 */
public class WriteToPropertiesSwingWithPaginationAndPageInfo {
    private Properties properties;
    private int addedCount = 0;
    private JFrame frame;
    private DefaultTableModel tableModel;
    private JTable table;
    private int currentPage = 1; // 当前页码
    private int itemsPerPage = 20; // 每页显示的项数
    // private static final int ROWS_PER_PAGE = 20;
    private JLabel pageInfoLabel;
    private JLabel pageInfoLabel1;

    public WriteToPropertiesSwingWithPaginationAndPageInfo() {
        properties = new Properties();

        try (InputStream inputStream = new FileInputStream("src/main/resources/config.properties")) {
            properties.load(new InputStreamReader(inputStream, "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建Swing界面
        frame = new JFrame("配置属性编辑器");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 400);
        frame.setLayout(new BorderLayout());

        // 创建表格模型和表格
        tableModel = new DefaultTableModel();
        tableModel.addColumn("键");
        tableModel.addColumn("值");
        table = new JTable(tableModel);

        // 创建滚动窗格以容纳表格
        JScrollPane tableScrollPane = new JScrollPane(table);

        // 添加分页控制按钮
        // 在分页面板中添加首页和末页按钮
        JButton firstPageButton = new JButton("首页");

        JButton prevButton = new JButton("上一页");
        JButton nextButton = new JButton("下一页");
        JButton lastPageButton = new JButton("末页");

        // 创建标签以显示页数信息
        pageInfoLabel = new JLabel();
        pageInfoLabel1 = new JLabel();

        // 创建面板以容纳分页按钮和标签
        JPanel paginationPanel = new JPanel();
        paginationPanel.add(firstPageButton);
        paginationPanel.add(prevButton);
        paginationPanel.add(nextButton);
        paginationPanel.add(lastPageButton);
        paginationPanel.add(pageInfoLabel);


        // 更新页数信息标签
        int maxPage = (int) Math.ceil((double) properties.size() / itemsPerPage);
        // pageInfoLabel.setText("页数: " + currentPage + " / " + maxPage);
        pageInfoLabel.setText("页数: ");


// 创建一个可编辑的文本框来显示和编辑当前页面
        JTextField currentPageTextField = new JTextField(String.valueOf(currentPage));
        paginationPanel.add(currentPageTextField);
        pageInfoLabel1.setText(" / " + maxPage);
        paginationPanel.add(pageInfoLabel1);

// 添加按钮用于手动跳转页面
        JButton jumpButton = new JButton("跳转");
        paginationPanel.add(jumpButton);


// 添加按钮的点击事件监听器
        jumpButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int newPage = Integer.parseInt(currentPageTextField.getText().trim());
                    if (newPage >= 1 && newPage <= maxPage) {
                        currentPage = newPage;
                        loadPropertiesToTableModel();
                    } else {
                        JOptionPane.showMessageDialog(frame, "无效的页数。", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(frame, "请输入有效的数字页数。", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
// 在分页面板中添加页数按钮
        for (int i = 1; i <= maxPage; i++) {
            JButton pageButton = new JButton(String.valueOf(i));
            paginationPanel.add(pageButton);

            // 添加页数按钮的点击事件监听器
            int finalI = i;
            pageButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (currentPage != finalI) {
                        currentPage = finalI;
                        loadPropertiesToTableModel();
                    }
                }
            });
//当用户点击页数按钮时，将更新当前页面文本框 (currentPageTextField) 的显示并切换到相应的页面。
            // 获取页数按钮上的文本
            String pageNumber = pageButton.getText();
// 将当前页文本框的值设置为页数按钮上的文本
            currentPageTextField.setText(pageNumber);
// 将当前页面变量 (currentPage) 更新为选择的页数
            currentPage = Integer.parseInt(pageNumber);
// 重新加载表格模型以显示选定的页面
            loadPropertiesToTableModel();
        }


        // 添加首页和末页按钮的点击事件监听器
        firstPageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (currentPage != 1) {
                    currentPage = 1;
                    loadPropertiesToTableModel();
                }
            }
        });

        lastPageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int maxPage = (int) Math.ceil((double) properties.size() / itemsPerPage);
                if (currentPage != maxPage) {
                    currentPage = maxPage;
                    loadPropertiesToTableModel();
                }
            }
        });

        // 添加分页按钮的点击事件监听器
        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (currentPage > 1) {
                    currentPage--;
                    loadPropertiesToTableModel();
                }
            }
        });

        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int maxPage = (int) Math.ceil((double) properties.size() / itemsPerPage);
                if (currentPage < maxPage) {
                    currentPage++;
                    loadPropertiesToTableModel();
                }
            }
        });

        // 将组件添加到界面
        frame.add(tableScrollPane, BorderLayout.CENTER);
        frame.add(paginationPanel, BorderLayout.SOUTH);

        // 显示初始的所有键值对
        loadPropertiesToTableModel();

        frame.setVisible(true);
    }
    // 在加载页面时，生成页数按钮并添加到 pageButtonPanel？？？
    private void loadPropertiesToTableModel() {
        // 清空表格模型
        tableModel.setRowCount(0);

        int startIndex = (currentPage - 1) * itemsPerPage;
        int endIndex = Math.min(startIndex + itemsPerPage, properties.size());

        int rowCount = 0;
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            if (rowCount >= startIndex && rowCount < endIndex) {
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                tableModel.addRow(new Object[]{key, value});
            }
            rowCount++;
        }


    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new WriteToPropertiesSwingWithPaginationAndPageInfo();
            }
        });
    }
}
